#pragma once

#include <iostream>
#include <memory>
#include <map>

using namespace std;


//抽象享元类NetworkDevice
class NetworkDevice{
public:
	virtual string getType() = 0;
	virtual void use() = 0;
}; 


//具体享元类Switch
class Switch : public NetworkDevice{
public:
	Switch(string type){
		this->type = type;
	}
	
	string getType(){
		return this->type;
	}
	
	void use(){
		cout << "Linked by switch,type is " << this->type << endl;
	}
private:
	string type;
}; 

//具体享元类Hub
class Hub : public NetworkDevice{
public:
	Hub(string type){
		this->type = type;
	}
	
	string getType(){
		return this->type;
	}
	
	void use(){
		cout << "Linked by Hub,type is " << this->type << endl;
	}
private:
	string type;
}; 


//享元类工厂类DeviceFactory
class DeviceFactory{
public:
	DeviceFactory(){
		shared_ptr<NetworkDevice> nd1 = make_shared<Switch>("Ciso-WS-C2950-24");
		devices.insert(make_pair("cisco",nd1));
		shared_ptr<NetworkDevice> nd2 = make_shared<Hub>("TP-LINK-HF8M");
		devices.insert(make_pair("tp",nd2));
	}
	
	shared_ptr<NetworkDevice> getNetworkDevice(string type){
		if(devices.count(type)){
			totalTerminal++;
			return devices.find(type)->second;
		}else{
			return nullptr;
		}
	}
	
	int getTotalDevice(){
		return devices.size();
	}
	
	int getTotalTerminal(){
		return totalTerminal;
	}
private:
	map<string,shared_ptr<NetworkDevice> > devices;
	int totalTerminal = 0;
};